package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Fyy
 * @since 2022/12/28 18:06
 */

@Service
public class AdServiceImpl implements AdService {
    @Autowired
    MarketAdMapper adMapper;

    @Override
    public CommonData<MarketAd> queryAll(BasePageInfo basePageInfo, String name, String content) {
        //分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        //对查询所需参数进行赋值
        MarketAdExample example = new MarketAdExample();
        MarketAdExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtil.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        //获取查询得到的值
        List<MarketAd> list = adMapper.selectByExample(example);
        PageInfo<MarketAd> marketAdPageInfo = new PageInfo<>(list);
        return CommonData.data(marketAdPageInfo);
    }

    @Override
    public void create(MarketAd marketAd) {
        adMapper.insertSelective(marketAd);
    }

    @Override
    public void update(MarketAd marketAd) {
        int affectedRows = adMapper.updateByPrimaryKeySelective(marketAd);
    }

    @Override
    public void delete(MarketAd marketAd) {
        MarketAd realMarketAd = new MarketAd();
        realMarketAd.setId(marketAd.getId());
        realMarketAd.setDeleted(true);
        adMapper.updateByPrimaryKeySelective(realMarketAd);
    }


}
